#include "WhyEngine.h"

WhyTime*			WhyEngine::time		= NULL;
WhyStateSystem*		WhyEngine::states	= NULL;
WhyGraphics*		WhyEngine::graphics = NULL;
WhyEventSystem*		WhyEngine::events	= NULL;
WhyInput*			WhyEngine::input	= NULL;

CRITICAL_SECTION	criticalSection;

void WhyEngine::PreInitialize()
{
	time = new WhyTime();
	states = new WhyStateSystem();
	graphics = new WhyGraphics();
	events = new WhyEventSystem();
	input = new WhyInput();

	InitializeCriticalSection(&criticalSection);
}

void WhyEngine::Initialize(HWND hwnd, HINSTANCE hInstance, int screenWidth, int screenHeight, bool isWindowed, bool vsync)
{
	graphics->Initialize(hwnd, screenWidth, screenHeight, isWindowed, vsync);
	input->Init(hwnd, hInstance);
	events->Init();
}

void WhyEngine::ShutDown()
{
	graphics->Shutdown();
	states->ShutDown();
	input->ShutDown();
	events->Shutdown();

	delete time;
	delete states;
	delete graphics;
	delete events;
	delete input;

	DeleteCriticalSection(&criticalSection);
}

bool WhyEngine::Main()
{
	time->Start();

	Update();
	Render();

	time->End();

	if(GetAsyncKeyState(VK_ESCAPE))
		return false;

	return true;
}

void WhyEngine::Update()
{
	input->Update();

	states->Update(time->GetDeltaTimeF());
}

void WhyEngine::Render()
{
	states->Render();
}